<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue3-005子组件ref</title>
    <script src="./vue3.js"></script>
</head>

<body>
    <div id="app">
        <child01 ref="child01" />
    </div>
    <script>
        const { createApp, defineComponent, ref, onMounted } = Vue

        const Child01 = defineComponent({
            template: `
                <div>Child01</div>
            `,
            setup() {
                return {
                    handle() {
                        alert('我是子组件的handle')
                    }
                }
            }
        })



        createApp({
            components: { Child01 },
            setup() {
                const child01 = ref()       // 1. 定义一个与组件ref命名相同的变量
                console.log(child01)        // 2. 此时变量value为空

                onMounted(_ => {
                    child01.value.handle()  // 4. 此时value值为当前的子组件
                })

                return {
                    child01                 // 3. 输出变量，给变量本身赋值为当前子组件
                }
            }
        }).mount("#app")
    </script>
</body>

</html>